/**
 * 
 */
package com.quiz.activity.questions;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.flurry.android.FlurryAgent;
import com.quiz.activity.PlaynowClickListener;
import com.quiz.activity.R;
import com.quiz.activity.sections.StartQuestionsClickListener;
import com.quiz.activity.util.QuizUtil;
import com.quiz.dao.SectionDataSource;
import com.quiz.model.Section;
import com.scoreloop.client.android.core.model.Score;
import com.scoreloop.client.android.ui.OnScoreSubmitObserver;
import com.scoreloop.client.android.ui.ScoreloopManagerSingleton;
import com.scoreloop.client.android.ui.ShowResultOverlayActivity;


/**
 *
 * @author Brian Boyle
 * Apr 15, 2012
 */
public class RoundSummaryActivity extends Activity implements OnScoreSubmitObserver {


    private SectionDataSource sectionDatasource;
    private Section currentSection;

    /**
     * {@inheritDoc}
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FlurryAgent.onStartSession(this, QuizUtil.FLURRY_KEY);

        setContentView(R.layout.round_completed);
        ScoreloopManagerSingleton.get().setOnScoreSubmitObserver(this);
        //Delete the current score and the number of lives left over for the last round of questions
        clearStoredValues();
        sectionDatasource = new SectionDataSource(this);

        TextView logo = (TextView) findViewById(R.id.logoText);
        QuizUtil.setMafiaFont(logo, 48, getAssets());

        final Bundle bundle = getIntent().getExtras();
        int correctQuestionCount = bundle.getInt("correctQuestionCount");
        currentSection = sectionDatasource.getSectionById(bundle.getLong("currentSectionId"));

        TextView correctQuestionsTextView = (TextView) findViewById(R.id.correctQuestionsText);
        correctQuestionsTextView.setText(correctQuestionCount + " / " + currentSection.getNumberOfQuestions());
        QuizUtil.setMafiaFont(correctQuestionsTextView, 64, getAssets());

        TextView pointsScoredTextView = (TextView) findViewById(R.id.pointsSummaryText);
        pointsScoredTextView.setText(QuizUtil.formatScore(bundle.getInt("pointsScored")) + " points");

        RelativeLayout layout = (RelativeLayout) findViewById(R.id.summaryView);
        if (bundle.getBoolean("playerHasNewRank")) {

            Section nextSection = sectionDatasource.getSectionById(bundle.getLong("nextSectionId"));

            //Log to Flurry the rank that has been achieved.
            FlurryAgent.logEvent(nextSection.getName());

            TextView congratsText = new TextView(this);
            congratsText.setText("Congratulations, you are now ranked");
            congratsText.setTextSize(16);
            congratsText.setId(200);

            RelativeLayout.LayoutParams congratsParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            congratsParams.setMargins(0, 14, 0, 0);
            congratsParams.addRule(RelativeLayout.CENTER_HORIZONTAL, 1);
            congratsParams.addRule(RelativeLayout.BELOW, R.id.pointsSummaryText);
            layout.addView(congratsText, congratsParams);

            TextView rankText = new TextView(this);
            rankText.setText(nextSection.getName());
            rankText.setId(300);
            QuizUtil.setMafiaFont(rankText, 56, getAssets());
            RelativeLayout.LayoutParams rankParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            rankParams.setMargins(0, 14, 0, 0);
            rankParams.addRule(RelativeLayout.CENTER_HORIZONTAL, 1);
            rankParams.addRule(RelativeLayout.BELOW, congratsText.getId());
            layout.addView(rankText, rankParams);

            Button playNextSectionButton = new Button(this);
            playNextSectionButton.setText("Play " + nextSection.getName() + " Questions");
            playNextSectionButton.setId(301);
            RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams(300, LayoutParams.WRAP_CONTENT);
            buttonParams.setMargins(0, 14, 0, 0);
            buttonParams.addRule(RelativeLayout.CENTER_HORIZONTAL, 1);
            buttonParams.addRule(RelativeLayout.BELOW, rankText.getId());
            layout.addView(playNextSectionButton, buttonParams);

            playNextSectionButton.setBackgroundResource(R.drawable.red_button);
            playNextSectionButton.setTextColor(Color.WHITE);
            playNextSectionButton.setTypeface(null, Typeface.BOLD);
            playNextSectionButton.setOnClickListener(new PlaynowClickListener(this, false));

            Button submitHighscore = new Button(this);
            submitHighscore.setText("Submit New Highscore");
            RelativeLayout.LayoutParams scoreButtonParams = new RelativeLayout.LayoutParams(300, LayoutParams.WRAP_CONTENT);
            scoreButtonParams.setMargins(0, 14, 0, 0);
            scoreButtonParams.addRule(RelativeLayout.CENTER_HORIZONTAL, 1);
            scoreButtonParams.addRule(RelativeLayout.BELOW, playNextSectionButton.getId());
            layout.addView(submitHighscore, scoreButtonParams);


            submitHighscore.setBackgroundResource(R.drawable.red_button);
            submitHighscore.setTextColor(Color.WHITE);
            submitHighscore.setTypeface(null, Typeface.BOLD);
            submitHighscore.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    Score score = new Score(new Double(bundle.getInt("pointsScored")), null);
                    //score.setLevel(1);
                    //score.setMode(getMode(currentSection));
                    ScoreloopManagerSingleton.get().onGamePlayEnded(score, false);
                }
            });


        } else {
            Button playAgainButton = new Button(this);
            playAgainButton.setText("Play Again");
            RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams(300, LayoutParams.WRAP_CONTENT);
            buttonParams.setMargins(0, 25, 0, 0);
            buttonParams.addRule(RelativeLayout.CENTER_HORIZONTAL, 1);
            buttonParams.addRule(RelativeLayout.BELOW, R.id.pointsSummaryText);
            layout.addView(playAgainButton, buttonParams);

            playAgainButton.setBackgroundResource(R.drawable.red_button);
            playAgainButton.setTextColor(Color.WHITE);
            playAgainButton.setTypeface(null, Typeface.BOLD);
            playAgainButton.setOnClickListener(new StartQuestionsClickListener(this, this.currentSection));
        }
    }

    private void clearStoredValues() {
        SharedPreferences settings = getSharedPreferences(QuestionActivity.PREF_FILE, MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.clear();
        editor.commit();
    }

    private int getMode(Section section) {
        return QuizUtil.determineSectionNumber(section.getName());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void onPause() {
        super.onPause();
        sectionDatasource.close();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void onResume() {
        super.onResume();
        sectionDatasource.open();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void onStop() {
        super.onStop();
        FlurryAgent.onEndSession(this);
    }

    /**
     * {@inheritDoc}
     */
    public void onScoreSubmit(int status, Exception error) {
        startActivity(new Intent(this, ShowResultOverlayActivity.class));
    }
}
